from typing_extensions import Annotated
from fastapi import Request, APIRouter, Depends, Path
from starlette.responses import JSONResponse

from core.formclass import AutoSaveForm
from core.models import Member
from service.ajax import AJAXService

router = APIRouter()


@router.get("/autosave_list")
async def autosave_list(
    request: Request,
    service: Annotated[AJAXService, Depends(AJAXService.async_init)],
):
    """
    返回自动保存列表.

    Args:
        request (Request): Request 对象
        service (AJAXService): Ajax 服务

    Returns:
        List[AutoSave]: 自动存储列表
    """
    member: Member = request.state.login_member
    service.validate_login(member)
    save_list = service.get_autosave_list(member)
    return save_list


@router.get("/autosave_count")
async def autosave_count(
    request: Request,
    service: Annotated[AJAXService, Depends(AJAXService.async_init)]
):
    """
    返回自动存储的数量.

    Args:
        request (Request): Request 对象
        service (AJAXService): Ajax 服务

    Returns:
        dict: 自动存储
    """
    member: Member = request.state.login_member
    service.validate_login(member)
    return {"count": service.get_autosave_count(member.mb_id)}


@router.get("/autosave_load/{as_id}")
async def autosave_load(
    request: Request,
    service: Annotated[AJAXService, Depends(AJAXService.async_init)],
    as_id: int = Path(..., title="自动存储 ID")
):
    """
    自动保存内容.

    Args:
        request (Request): Request 对象
        service (AJAXService): Ajax 服务
        as_id (int, optional): 自动存储 ID.

    Returns:
        AutoSave: 自动存储数据

    Raises:
        JSONException: 我需要登录
        JSONException: 如果没有存储的写入
        JSONException: 如果您没有访问权限
    """
    member: Member = request.state.login_member
    service.validate_login(member)
    save_data = service.get_autosave_content(as_id, member)
    return save_data


@router.post("/autosave")
async def autosave(
    request: Request,
    service: Annotated[AJAXService, Depends(AJAXService.async_init)],
    form_data: AutoSaveForm = Depends()
):
    """
    书写的临时存储

    Args:
        request (Request): Request 对象
        service (AJAXService): Ajax 服务
        form_data (AutoSaveForm, optional): 自动存储数据.

    Returns:
        JSONResponse: 临时存储的数量

    Raises:
        JSONException: 我需要登录
    """
    member: Member = request.state.login_member
    service.validate_login(member)
    service.autosave_save(member, form_data)
    count = service.get_autosave_count(member.mb_id)
    return JSONResponse(status_code=201, content={"count": count})


@router.delete("/autosave/{as_id}")
async def delete_autosave(
    request: Request,
    service: Annotated[AJAXService, Depends(AJAXService.async_init)],
    as_id: int = Path(..., title="自动存储 ID")
):
    """
    删除临时存储

    Args:
        request (Request): Request 对象
        service (AJAXService): Ajax 服务
        as_id (int, optional): 自动存储 ID.

    Returns:
        JSONResponse: 已被删除.

    Raises:
        JSONException: 我需要登录
        JSONException: 如果没有存储的写入
        JSONException: 如果您没有访问权限
    """
    member: Member = request.state.login_member
    service.validate_login(member)
    service.autosave_delete(as_id, member)
    return JSONResponse(status_code=200, content="已被删除.")
